<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>仿真结果概览</title>
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/bootstrap-select.css" rel="stylesheet">
<link href="../css/font-awesome.min.css" rel="stylesheet">
<link href="../css/JyyCommon.css" rel="stylesheet">
<link href="../css/JyyTable.css" rel="stylesheet">
<link href="../css/JyyTree.css" rel="stylesheet">
<link href="../css/JyyMsgBox.css" rel="stylesheet">
<script type="text/javascript" src="../js/jquery-3.2.1.js"></script>
<script type="text/javascript" src="../js/jquery.form.min.js"></script> 
<script type="text/javascript" src="../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../js/bootstrap-paginator.min.js"></script>
<script type="text/javascript" src="../js/bootstrap-select.js"></script>
<script type="text/javascript" src="../js/JyyCommon.js"></script>
<script type="text/javascript" src="../js/JyyTable.js"></script>
<script type="text/javascript" src="../js/JyyTree.js"></script>
<script type="text/javascript" src="../js/JyyMsgBox.js"></script>
<script type="text/javascript" src="../js/JyyLoading.js"></script>
<style type="text/css">
.heidenbt{
	display: none
}
.col-sm-3 div {
	z-index: 10000;
}
</style>
<script type="text/javascript">
var table = null;
function getQueryString (name){
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	return r ? unescape(r[2]) : null;
}
var code =  this.getQueryString("code");
var planId =  this.getQueryString("projectId")||'88888888888';
$(function(){
	$.when(getTableData(code)).done(function(tableData){
		if(table == null){
			table = JyyTable.init("table", tableData, {position:"absolute",top:"50px",left:"1px",right:"5px",bottom:"0px",overflow:"hidden"}, onTableEvent);
			table.hideCols(true);
		}else{
			table.refresh(tableData);
			table.hideCols(true);
		}
	});
	setlabel();

	$("#valuetype").change(function() {
		var value = $(this).val();
		$.ajax({
	        type: 'post',
			url: "../pv/page/pageInfo/getTermData",
			data: {"projectid":planId, "valuetype":value},
			dataType: "json",
			async:true,
	        success: function (data) {
	        	//拼接下拉框
	            $("#term").empty();
	    		$("#term").append("<option></option>");
	            for(var i=0;i<data.length;i++){
	                $("#term").append("<option value='"+data[i].CODE+"'>"+data[i].NAME+"</option>");
	            }
	            //这一步不要忘记 不然下拉框没有数据
	            $("#term").selectpicker("refresh");
	        }
	    });
	});
});

function setlabel(){
	var label = "";
	var arr = [];
	if(code == "4000001"){
		label = "机组名称";
		arr = [{"id":"20","value":"火电装机"},{"id":"21","value":"风电装机"},{"id":"22","value":"光伏装机"},
			   {"id":"23","value":"光热装机"},{"id":"24","value":"电储能装机"}];
		$("#valuetype").empty();
		$("#valuetype").append("<option></option>");
		for(var i = 0;i < arr.length;i++) {
            $("#valuetype").append("<option value='"+arr[i].id+"'>"+arr[i].value+"</option>");
		}
	} else if(code == "4000002"){
		label = "线路名称";
		$("#valuetypeDiv").addClass("heidenbt");
		$("#typelabel").addClass("heidenbt");
		$.ajax({
	        type: 'post',
			url: "../pv/page/pageInfo/getTermData",
			data: {"projectid":planId, "valuetype":25},
			dataType: "json",
			async:true,
	        success: function (data) {
	        	//拼接下拉框
	            $("#term").empty();
	    		$("#term").append("<option></option>");
	            for(var i=0;i<data.length;i++){
	                $("#term").append("<option value='"+data[i].CODE+"'>"+data[i].NAME+"</option>");
	            }
	            //这一步不要忘记 不然下拉框没有数据
	            $("#term").selectpicker("refresh");
	        }
	    });
	} else if(code == "4000003") {
		label = "机组名称";
		arr = [{"id":"1","value":"火电出力"},{"id":"2","value":"风电出力"},{"id":"3","value":"光伏出力"},
			   {"id":"4","value":"弃风曲线"},{"id":"5","value":"弃光曲线"},{"id":"13","value":"光热出力"},
			   {"id":"14","value":"储热吸热曲线"},{"id":"15","value":"储热放热曲线"},{"id":"16","value":"储热SOC"},
			   {"id":"17","value":"电储能充电曲线"},{"id":"18","value":"电储能放电曲线"},
			   {"id":"19","value":"电储能SOC"},{"id":"27","value":"水电出力"}];
		$("#valuetype").empty();
		$("#valuetype").append("<option></option>");
		for(var i = 0;i < arr.length;i++) {
            $("#valuetype").append("<option value='"+arr[i].id+"'>"+arr[i].value+"</option>");
		}
	} else if(code == "4000004") {
		label = "节点名称";
		arr = [{"id":"6","value":"切负荷曲线"},{"id":"12","value":"节点相角"}];
		$("#valuetype").empty();
		$("#valuetype").append("<option></option>");
		for(var i = 0;i < arr.length;i++) {
            $("#valuetype").append("<option value='"+arr[i].id+"'>"+arr[i].value+"</option>");
		}
	} else if(code == "4000005") {
		label = "类别名称";
		arr = [{"id":"7","value":"在线容量"},{"id":"8","value":"开机容量"},
			   {"id":"9","value":"关机容量"},{"id":"10","value":"启停成本"}];
		$("#valuetype").empty();
		$("#valuetype").append("<option></option>");
		for(var i = 0;i < arr.length;i++) {
            $("#valuetype").append("<option value='"+arr[i].id+"'>"+arr[i].value+"</option>");
		}
	} else if(code == "4000006"){
		label = "线路名称";
		$("#valuetypeDiv").addClass("heidenbt");
		$("#typelabel").addClass("heidenbt");
		$.ajax({
	        type: 'post',
			url: "../pv/page/pageInfo/getTermData",
			data: {"projectid":planId, "valuetype":11},
			dataType: "json",
			async:true,
	        success: function (data) {
	        	//拼接下拉框
	            $("#term").empty();
	    		$("#term").append("<option></option>");
	            for(var i=0;i<data.length;i++){
	                $("#term").append("<option value='"+data[i].CODE+"'>"+data[i].NAME+"</option>");
	            }
	            //这一步不要忘记 不然下拉框没有数据
	            $("#term").selectpicker("refresh");
	        }
	    });
	}
    $("#valuetype").selectpicker("refresh");
	$("#searchName").text(label);
}

function getTableData(code, currentPage, pageSize, valueid, valuetype){
	var defer = $.Deferred();
	var result = null;
	var data = {"pagenow":currentPage,"pagenum":pageSize,"valueid":valueid,"valuetype":valuetype,"projectid":planId,"code":code};
	
	$.ajax({
		dataType: "json",
		async:true,
		data:data,
		url: "../pv/page/pageInfo/resultTable"
	}).done(function(res) {
		var sts=eval("("+res+")");
		result = sts;
		defer.resolve(sts);
	});
	return defer.promise();
}

//下载按钮
function download(){
	var form = $('<form action="../pv/pageExcel/downloadExcel/'+ code +'" style="display:none" method="post">');
	var input = $('<input type="hidden" name="projectid">');
	input.attr("value",planId);
	form.append(input);
	$("body").append(form);
	form.submit();
	form.remove();
}
//查询
function select(){
	
	var value = $("#term").val();
	var value1 = $("#valuetype").val();
	
	var currentPage = 1;
	var pageSize = table.config.page.option.pageSize;
	
	JyyLoading.start();
	$.when(getTableData(code, currentPage, pageSize, value, value1)).done(function(tableData){
		table.refresh(tableData);
		JyyLoading.end();
	});
}
//重置
function reset(){
	$("#valuetype").val("");
    $("#valuetype").selectpicker("refresh");
	$("#term").val("");
	$("#term").empty();
	$("#term").append("<option></option>");
    $("#term").selectpicker("refresh");
	
	var currentPage = 1;
	var pageSize = table.config.page.option.pageSize;
	
	JyyLoading.start();
	$.when(getTableData(code, currentPage, pageSize, null, null)).done(function(tableData){
		table.refresh(tableData);
		JyyLoading.end();
	});
}

function onTableEvent(type, value){
	console.log("onTableEvent");
	if(type == "insertBefore"){
		console.log("insertBefore");
		//核查数据正确性
		for(var a = 0 ; a < value[1].length; a++){
			if(table.config.hideCols.indexOf(a) <= -1){
				if(JyyCommon.isEmpty(value[1][a])){
					JyyMsgBox.alert({ title:"警告", message:"["+table.config.data[0][a-1].name+"]不可为空",type:"warning"}).on(function (e) {});
					return false;
				}else if((!JyyCommon.isObject(value[1][a]) && value[1][a].indexOf("请选择")>-1) || value[1][a] == " "){
					JyyMsgBox.alert({ title:"警告", message:"["+table.config.data[0][a-1].name+"]下拉框未做选择",type:"warning"}).on(function (e) {});
					return false;
				}else if(JyyCommon.isObject(value[1][a]) && JyyCommon.isEmpty(value[1][a].name)){
					JyyMsgBox.alert({ title:"警告", message:"["+table.config.data[0][a-1].name+"]不可为空",type:"warning"}).on(function (e) {});
					return false;
				}
			}
		}
		return true;
	} else if(type == "data"){
		JyyLoading.start();
			 $.ajax({
            type:"POST",
            url:"../pv/page/check/"+code,
            data:{"value":JSON.stringify(value),"projectid":planId},
            success:function(data){
            	var pageSize = table.config.page.option.pageSize;
				$.when(getTableData(code, 1, pageSize, $("#term").val(),$("#valuetype").val())).done(function(tableData){
					table.refresh(tableData);
					JyyLoading.end();
				});
            }
        }); 
	} else if(type == "page"){
		console.log("page");
		var val = $("#term").val();
		var val1 = $("#valuetype").val();
		JyyLoading.start();
		$.when(getTableData(code, value.currentPage, value.pageSize, val, val1)).done(function(tableData){
			table.refresh(tableData);
			JyyLoading.end();
		});
	}
}

</script>
</head>
<body>
<div id="operate" style="height: 50px;">
	<div class="col-md-12" style="position: absolute;top: 10px;padding-left: 5px;padding-right: 5px;">
		
		<button id="daochu"  class="btn btn-info btn-sm" onclick=download()><i class="glyphicon glyphicon-download"></i>导出Excel</button>
		
		<button class="btn btn-info btn-sm" style="float: right;" onclick=reset()>重置</button>
		<button class="btn btn-info btn-sm" style="float: right;margin-right: 5px;" onclick=select()>查询</button>

		<div class="col-sm-3" style="float: right;width:170px;padding-left: 0px;">
			<select id="term" title="请选择.." class="selectpicker form-control" data-width="150px" style="width:150px;height:30px;font-size: 12px;" id="txt_search_departmentname">
           		<option></option>
           	</select>
        </div>
		<label id="searchName" class="control-label col-sm-1" style="float: right;margin-top: 7px;padding: 0px;width: 60px;font-weight: normal;" for="txt_search_departmentname"></label>
		<div id="valuetypeDiv" class="col-sm-3" style="float: right;width:170px;padding-left: 0px;">
           	<select id="valuetype" title="请选择.." class="selectpicker form-control" data-width="150px" style="width:150px;height:30px;font-size: 12px;" id="txt_search_valuetype">
           	</select>
        </div>
		<label id="typelabel" class="control-label col-sm-1" style="float: right;margin-top: 7px;padding: 0px;width: 60px;font-weight: normal;" for="txt_search_valuetype">数据类型</label>
	</div>
</div>
<div id="table"></div>
</body>
</html>